var t = require("../../utils/public");
var app = getApp();
var status = require('../../utils/index.js');
var util = require('../../utils/util.js');

Component({
    properties: {
        spuItem: {
            type: Object,
            value: {
                spuId: "",
                skuId: "",
                spuImage: "",
                spuName: "",
                endTime: 0,
                beginTime: "",
                actPrice: ["", ""],
                marketPrice: ["", ""],
                spuCanBuyNum: "",
                soldNum: "",
                actId: "",
                limitMemberNum: "",
                limitOrderNum: "",
                serverTime: "",
                isLimit: false,
                skuList: [],
                spuDescribe: "",
                is_take_fullreduction: 0,
                bigImg: '',
                car_count: 0
            }
        },
        spuItemList:{
            type: Object
        },
        isPast: {
            type: Boolean,
            value: false
        },
        actEnd: {
            type: Boolean,
            value: false
        },
        reduction: {
            type: Object,
            value: {
                full_money: '',
                full_reducemoney: '',
                is_open_fullreduction: 0
            }
        },
        isShowListCount: {
            type: Number,
            value: 0
        },
        shopType: {
            type: Number,
            value: 0
        },
        needAuth: {
            type: Boolean,
            value: false
        },
        is_open_vipcard_buy: {
            type: Number,
            value: 0
        },
        canLevelBuy: {
            type: Boolean,
            value: false
        },
        showPickTime: {
            type: Boolean,
            value: false
        },
        skin: {
            type: Object
        },
        saleUnit: {
            type: String
        }
    },
    attached() {
        this.setData({
            placeholdeImg: app.globalData.placeholdeImg
        })
    },
    data: {
        disabled: false,
        placeholdeImg: '',
        number: 0,
        url: ''
    },
    ready: function() {},
    methods: {
        playVideo: function(e) {
            var id = e.currentTarget.dataset.id,
                that = this,
                token = wx.getStorageSync('token'),
                spuItem = that.data.spuItem;
            app.util.request({
                'url': 'entry/wxapp/index',
                'data': {
                    controller: 'recipe.play_recipe_do',
                    token: token,
                    id: spuItem.id
                },
                dataType: 'json',
                success: function(res) {
                    if (res.data.code == 0) {
                        that.triggerEvent('stopOtherPlay');
                        var up = "spuItem.showVideo";
                        that.setData({
                            [up]: false
                        })
                        // that.videoContext = wx.createVideoContext(id);
                        // that.videoContext.play();
                        
                        spuItem.playnum = res.data.playnum;
                        that.setData({
                            spuItem
                        })
                    } else if (res.data.code == 1) {
                        //未登录
                        that.triggerEvent('authModal');
                    }
                }
            })
        },
        videEndPlay: function(e) {
            var index = e.currentTarget.dataset.index;
            var up = "spuItem.showVideo";
            this.setData({
                [up]: false
            })
        },
        goDetail: function(e) {
            let idx = e.currentTarget.dataset.id;
            wx.navigateTo({
                url: '/lionfish_comshop/moduleA/menu/details?&id=' + idx,
            })
        },
        onShareAppMessage: function() {
            return {
                path: "lionfish_comshop/moduleA/menu/other",
                success: function() {},
                fail: function() {}
            };
        },
        agree: function(t) {
            let that = this;
            let token = wx.getStorageSync('token');
            let spuItem = this.data.spuItem;

            app.util.request({
                'url': 'entry/wxapp/index',
                'data': {
                    controller: 'recipe.fav_recipe_do',
                    token: token,
                    id: spuItem.id
                },
                dataType: 'json',
                success: function(res) {
                    if (res.data.code == 0) {
                        //成功
                        wx.showToast({
                            title: '已喜欢~',
                            icon: 'none'
                        })
                        spuItem.fav_count = res.data.fav_count;
                        spuItem.has_fav = 1;
                        that.setData({
                            spuItem
                        })
                    } else if (res.data.code == 1) {
                        //未登录
                        that.triggerEvent('authModal');
                    } else if (res.data.code == 2) {
                        //取消收藏
                        spuItem.fav_count = res.data.fav_count;
                        spuItem.has_fav = 0;
                        that.setData({
                            spuItem
                        })
                        wx.showToast({
                            title: '取消喜欢~',
                            icon: 'none'
                        })
                    }
                }
            })
        },
    }
});